package cn.com.customer.manager.ui.edit

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import cn.com.customer.manager.R
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.manager.LoginManager
import cn.com.customer.manager.ui.regist.FindPasswordActivity
import cn.com.customer.manager.utils.PictrueSelectUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.entity.LocalMedia
import com.xuexiang.xui.widget.imageview.strategy.ILoadListener
import com.xuexiang.xui.widget.imageview.strategy.impl.GlideImageLoadStrategy
import com.xuexiang.xutil.display.ImageUtils
import kotlinx.android.synthetic.main.activity_edit_info.*

class EditInfoActivity : BaseActivity(), EditInfoContract.View {
    private lateinit var presenter: EditInfoPresenter
    var user=LoginManager.getInstance(this).user
    var picturePath:String=""
    private var mSelectList: List<LocalMedia> = mutableListOf()
    override fun getLayoutId(): Int {
        return R.layout.activity_edit_info
    }

    override fun initView(savedInstanceState: Bundle?) {
        initActionBar(getString(R.string.edit_info))
        user?.let {
            if (!it.headUrl.isNullOrEmpty()){
                GlideImageLoadStrategy().loadImage(iv_photo,it.headUrl,object : ILoadListener {
                    override fun onLoadFailed(error: Throwable?) {
                        iv_photo.setImageResource(R.drawable.photo_icon)
                    }
                    override fun onLoadSuccess() {
                    }

                })   }
            tv_use_name.setText(it.name)
            if (!it.deptName.isNullOrEmpty()){
                tv_job_name.setText(it.deptName)
            }
            tv_email.setText(it.email)
        }
        presenter = EditInfoPresenter(this)
        button_submit.setOnClickListener(this)
        ll_photo.setOnClickListener(this)
        ll_update_pwd.setOnClickListener(this)
    }

    override fun modifySuccess() {
        user?.let { LoginManager.getInstance(this).saveUser(it) }
        finish()
    }

    override fun setPresenter(presenter: EditInfoPresenter) {
        this.presenter = presenter

    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v) {
            button_submit -> {
                user?.let {
                    it.name = tv_use_name.text.toString()
                    it.deptName= tv_job_name.text.toString()
                    it.email=tv_email.text.toString()
                    presenter.modifyInfo(it,picturePath)
                }

            }
            ll_photo -> {
                PictrueSelectUtils.getPictureSelector(this)
                        .selectionMedia(mSelectList)
                        .maxSelectNum(1)
                        .selectionMode(PictureConfig.SINGLE)
                        .forResult(PictureConfig.CHOOSE_REQUEST)
            }
            ll_update_pwd -> {
             startActivity(Intent(this,FindPasswordActivity::class.java))
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    // 图片选择
                    mSelectList = PictureSelector.obtainMultipleResult(data)
                    picturePath= mSelectList[0].path
                    iv_photo.setImageBitmap(ImageUtils.getBitmap(picturePath))
                }
                else -> {
                }
            }
        }
    }
}